package org.fastsyncer.connector.sql;

import org.fastsyncer.connector.config.DatabaseConfig;
import org.fastsyncer.connector.config.SqlConfig;
import org.fastsyncer.connector.constant.DatabaseConstant;
import org.fastsyncer.connector.database.AbstractDatabaseConnector;

public final class DQLOracleConnector extends AbstractDatabaseConnector {

    @Override
    public SqlConfig getSqlConfig(DatabaseConfig config, String table, String queryFilter) {
        // SELECT * FROM USER WHERE ID = '123'
        String sql = config.getSql();
        // SELECT * FROM USER WHERE ROWNUM = 0
        // SELECT * FROM USER WHERE ID = '123' AND ROWNUM = 0
        String metaSql = new StringBuilder().append(sql).append(sql.contains(" WHERE ") ? " AND" : " WHERE").append(DatabaseConstant.ORACLE_PAGE_SQL_NULL).toString();
        return new SqlConfig(metaSql, sql);
    }

    @Override
    public String getPageSql(DatabaseConfig config, String table, String sql) {
        // Oracle 分页查询
        return DatabaseConstant.ORACLE_PAGE_SQL_START + sql + DatabaseConstant.ORACLE_PAGE_SQL_END;
    }

}
